package com.popupwindowdemo;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Locale;


/**
 * Created by Administrator on 2017/3/14.
 */
public class PopupWindowNumber extends PopupWindow {

    private int  LastTimer = 0;
    private int recLen = 0;
    private TextView TvTaskTime;

//    private Chronometer Timer;


    private RelativeLayout rl_Numbe_dxt;
    private RelativeLayout rl_Numbe_wxxz;
    private RelativeLayout rl_Numbe_ydxz;
    private RelativeLayout rl_Numbe_tlxz;

    private Button[] btn_Number_dxt;
    private Button[] btn_Number_wxxz;
    private Button[] btn_Number_ydxz;
    private Button[] btn_Number_tlxz;

    private int i_sum_dxt;
    private int i_sum_wxxz;
    private int i_sum_ydxz;
    private int i_sum_tlxz;

    private TextView TvClose;
    private View mMenuView;

    public PopupWindowNumber(Activity context) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.pop_number, null);

        TvTaskTime = (TextView) mMenuView.findViewById(R.id.tv_task_time);
        LastTimer = 560;
        handler.postDelayed(runnable, 1000);

/*        Timer = (Chronometer) mMenuView.findViewById(R.id.timer);
        Timer.setBase(SystemClock.elapsedRealtime());//计时器清零
        int hour = (int) ((SystemClock.elapsedRealtime() - Timer.getBase()) / 1000 / 60);
        Timer.setFormat("0"+String.valueOf(hour)+":%s");
        Timer.start();*/



        TvClose = (TextView) mMenuView.findViewById(R.id.textview_close);
        //取消按钮
        TvClose.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                //销毁弹出框
                dismiss();
            }
        });
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.Popupwindow);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
//        mMenuView.setOnTouchListener(new OnTouchListener() {
//
//            public boolean onTouch(View v, MotionEvent event) {
//
//                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
//                int y=(int) event.getY();
//                if(event.getAction()==MotionEvent.ACTION_UP){
//                    if(y<height){
//                        dismiss();
//                    }
//                }
//                return true;
//            }
//        });

//        i_sum = PaperAnalyzeInfor.get(0).getQuesList().size();

        i_sum_dxt = 7;
        initViewDXT(context);

        i_sum_wxxz = 10;
        initViewWXXZ(context);

        i_sum_ydxz = 6;
        initViewYDXZ(context);

        i_sum_tlxz = 7;
        initViewTLXZ(context);

    }

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            recLen++;
            TvTaskTime.setText(getStringTime(LastTimer+recLen));
            handler.postDelayed(this, 1000);
        }
    };

    private String getStringTime(int cnt) {
        int hour = cnt/3600;
        int min = cnt % 3600 / 60;
        int second = cnt % 60;
        return String.format(Locale.CHINA,"%02d:%02d:%02d",hour,min,second);
    }




    private void initViewDXT(Activity mContext){

//        TvQuesType = (Button) mMenuView.findViewById(R.id.tv_single_choice);
//        TvQuesType.setText("单选题");

        DisplayMetrics dm = new DisplayMetrics();
        mContext.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;

        rl_Numbe_dxt = (RelativeLayout) mMenuView.findViewById(R.id.rlayout_number_dxt);
        btn_Number_dxt = new Button[i_sum_dxt];

        int j = -1;
        for(int i=0;i<i_sum_dxt;i++){
            btn_Number_dxt[i] = new Button(mContext);
            btn_Number_dxt[i].setText(String.valueOf(i+1));
            btn_Number_dxt[i].setTextSize(10);
            final int finalI = i;
            btn_Number_dxt[i].setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
//                    TestPaperCommonActivity.VpTestPaperCommonContent.setCurrentItem(finalI);
                    dismiss();
                }
            });
            RelativeLayout.LayoutParams btnParams = new RelativeLayout.LayoutParams(width/9, width/9);
//            RelativeLayout.LayoutParams btnParams = new RelativeLayout.LayoutParams(Control.dip2px(mContext,width/18), Control.dip2px(mContext,width/18));

            if(i%5 == 0){
                j++;
            }

//            btnParams.leftMargin =((width)/5)*(i%5)+(width/5-width/9)/2;   //横坐标定位
//            btnParams.topMargin = 20 + (width/9+10)*j;   //纵坐标定位

            btnParams.leftMargin =((width)/5)*(i%5)+(width/5-width/9)/2;   //横坐标定位
            btnParams.topMargin = 20 + (width/9+20)*j;   //纵坐标定位

//            boolean bAnswerFinishAll = true;
//            String strStudentAnswer = "";
//            String strSelfAssScore = "";
//            int sumStudentAnswer = 0;
//
//            strStudentAnswer = PaperAnalyzeInfor.get(0).getQuesList().get(i).getStuAnswer();
//            strSelfAssScore = PaperAnalyzeInfor.get(0).getQuesList().get(i).getSelfAssScore();
//            sumStudentAnswer =  PaperAnalyzeInfor.get(0).getQuesList().get(i).getAllCount();
//
//            String[] arrayStudentAnswer = new String[sumStudentAnswer];
//            if(strStudentAnswer.equals("")){
//                for(int k = 0;k<sumStudentAnswer;k++){
//                    arrayStudentAnswer[k] = "";
//                }
//            }else{
//                for(int k = 0;k<sumStudentAnswer;k++) {
//                    arrayStudentAnswer[k] = Control.returnArray(strStudentAnswer, "|", 1)[k];
//                }
//            }
//
//            String[] arraySelfAssScore = new String[sumStudentAnswer];
//            if(strSelfAssScore.equals("")){
//                for(int k = 0;k<sumStudentAnswer;k++){
//                    arraySelfAssScore[k] = "";
//                }
//            }else{
//                for(int k = 0;k<sumStudentAnswer;k++) {
//                    arraySelfAssScore[k] = Control.returnArray(strSelfAssScore, "|", 1)[k];
//                }
//            }
//
//            for(int k = 0;k<sumStudentAnswer;k++){
//                if(arrayStudentAnswer[k].equals("") && arraySelfAssScore[k].equals("")){
//                    bAnswerFinishAll = false;
//                    break;
//                }
//
//            }
//
//            if(!bAnswerFinishAll){
//
//                btn_QuestinNumber[i].setBackgroundResource(R.drawable.round_number_unfocus_bg);
//                btn_QuestinNumber[i].setTextColor(Color.parseColor("#00E500"));
//            }else{
//
//                btn_QuestinNumber[i].setBackgroundResource(R.drawable.round_number_focus_bg);
//                btn_QuestinNumber[i].setTextColor(Color.parseColor("#FFFFFF"));
//            }


            rl_Numbe_dxt.addView(btn_Number_dxt[i],btnParams);

        }

    }

    /*
    *  完型选择
    * */
    private void initViewWXXZ(Activity mContext){

        DisplayMetrics dm = new DisplayMetrics();
        mContext.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;

        rl_Numbe_wxxz = (RelativeLayout) mMenuView.findViewById(R.id.rlayout_number_wxxz);
        btn_Number_wxxz = new Button[i_sum_wxxz];

        int j = -1;
        for(int i=0;i<i_sum_wxxz;i++){
            btn_Number_wxxz[i] = new Button(mContext);
//            btn_Number_wxxz[i].setText(String.valueOf(i+1+i_sum_dxt));
            btn_Number_wxxz[i].setText(String.valueOf(i_sum_dxt+1)+"-"+String.valueOf(i+1));
            btn_Number_wxxz[i].setTextSize(10);
            final int finalI = i;
            btn_Number_wxxz[i].setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
            RelativeLayout.LayoutParams btnParams = new RelativeLayout.LayoutParams(width/9, width/9);

            if(i%5 == 0){
                j++;
            }

            btnParams.leftMargin =((width)/5)*(i%5)+(width/5-width/9)/2;   //横坐标定位
            btnParams.topMargin = 20 + (width/9+20)*j;   //纵坐标定位

            rl_Numbe_wxxz.addView(btn_Number_wxxz[i],btnParams);

        }

    }

    /*
    * 阅读选择
    * */
    private void initViewYDXZ(Activity mContext){

        DisplayMetrics dm = new DisplayMetrics();
        mContext.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;

        rl_Numbe_ydxz = (RelativeLayout) mMenuView.findViewById(R.id.rlayout_number_ydxz);
        btn_Number_ydxz = new Button[i_sum_ydxz];

        int j = -1;
        for(int i=0;i<i_sum_ydxz;i++){
            btn_Number_ydxz[i] = new Button(mContext);
//            btn_Number_ydxz[i].setText(String.valueOf(i+1+i_sum_dxt+i_sum_wxxz));
            btn_Number_ydxz[i].setText(String.valueOf(i+1+i_sum_dxt+1));
            btn_Number_ydxz[i].setTextSize(10);
            final int finalI = i;
            btn_Number_ydxz[i].setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
            RelativeLayout.LayoutParams btnParams = new RelativeLayout.LayoutParams(width/9, width/9);

            if(i%5 == 0){
                j++;
            }

            btnParams.leftMargin =((width)/5)*(i%5)+(width/5-width/9)/2;   //横坐标定位
            btnParams.topMargin = 20 + (width/9+20)*j;   //纵坐标定位

            rl_Numbe_ydxz.addView(btn_Number_ydxz[i],btnParams);

        }

    }


    /*
    * 听力选择
    * */
    private void initViewTLXZ(Activity mContext){

        DisplayMetrics dm = new DisplayMetrics();
        mContext.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;

        rl_Numbe_tlxz = (RelativeLayout) mMenuView.findViewById(R.id.rlayout_number_tlxz);
        btn_Number_tlxz = new Button[i_sum_tlxz];

        int j = -1;
        for(int i=0;i<i_sum_tlxz;i++){
            btn_Number_tlxz[i] = new Button(mContext);
//            btn_Number_tlxz[i].setText(String.valueOf(i+1+i_sum_dxt+i_sum_wxxz+i_sum_ydxz));
            btn_Number_tlxz[i].setText(String.valueOf(i+1+i_sum_dxt+1+i_sum_ydxz));
            btn_Number_tlxz[i].setTextSize(10);
            final int finalI = i;
            btn_Number_tlxz[i].setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
            RelativeLayout.LayoutParams btnParams = new RelativeLayout.LayoutParams(width/9, width/9);

            if(i%5 == 0){
                j++;
            }

            btnParams.leftMargin =((width)/5)*(i%5)+(width/5-width/9)/2;   //横坐标定位
            btnParams.topMargin = 20 + (width/9+20)*j;   //纵坐标定位

            rl_Numbe_tlxz.addView(btn_Number_tlxz[i],btnParams);

        }

    }


}